import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;

public class PathFindTest extends JFrame {
    int step = 5; //这里不要改为3，4，6，7这些。。。。改为5比较容易匹配
    static int windowWidth = 1198;
    static int windowHeight = 891;
    Image buffer;  // Map会往这个buffer画内容
    Graphics bufferGraphics;  // 双缓冲图像的绘图上下文
    Graphics graphics;
    BufferedImage bufferedImage;
    int width;
    int height;
    Map map;
    Color drawFoundPathColor = new Color(255,0,0);
    Color drawLineColor = new Color(0,0,255);
    Color drawFindingColor = new Color(0,255,0);
    Coord start;
    Coord end;
    int drawSize = 5;

    //打印日志工具，可以看到打印时当前类，方法，行，比便方便，sout不能看到这些信息不太方便
    Logger logger = MyLogger.getLogger();

    PathFindTest()
    {

        setSize(windowWidth, windowHeight);

        // 窗口居中
        setLocationRelativeTo(null);

        // 关闭事件
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 用户不能调整窗口大小
        setResizable(true);

        // 标题
        setTitle("A Star");

        // 窗口可见
        setVisible(true);

        graphics = getGraphics();
        start = new Coord(0,0);
        end = new Coord(0,0);

        Init();

        map.resetBuffer();

        graphics.drawImage(bufferedImage, 0, 0, width, height, null);

        addMouseListener();
    }

    void Init()
    {
        try {
            bufferedImage = ImageIO.read(new File("img/Map_zoom_out_5_2.jpg"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        buffer = createImage(bufferedImage.getWidth(), bufferedImage.getHeight());
        bufferGraphics = buffer.getGraphics();
        width = bufferedImage.getWidth();
        height = bufferedImage.getHeight();
        logger.info(height + ": " + width);
        map = new Map(bufferedImage, buffer);
    }

    void addMouseListener()
    {
        addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                logger.info(e.getX() + ", " + e.getY());
                if (start.x == 0)
                {

                    start.x = e.getX() / step * step;
                    start.y = e.getY() / step * step;

                    bufferGraphics.setColor(new Color(0, 255, 0));
                    bufferGraphics.fillRect(start.x, start.y, drawSize, drawSize);

                    graphics.drawImage(buffer, 0, 0, width, height, null);

                    if (map.data[start.y][start.x] == MapUnit.BAR) //非路，不可以走
                        start.x = 0;
                }
                else
                {
                    end.x = e.getX() / step * step;
                    end.y = e.getY() / step * step;

                    bufferGraphics.setColor(new Color(0, 255, 0));
                    bufferGraphics.fillRect(end.x, end.y, drawSize, drawSize);

                    bufferGraphics.setColor(drawLineColor);
                    bufferGraphics.drawLine(start.x, start.y, end.x, end.y);

                    if (map.data[end.y][end.x] == MapUnit.BAR)//非路，不可以走
                    {
                        return;
                    }
                    map.resetBuffer();
                    graphics.drawImage(buffer, 0, 0, width, height, null);
                    ArrayList<Coord> downPathToTargetRedList;
                    map.resetBuffer();
                    downPathToTargetRedList = map.pathFindInit(start, end);
                    Collections.reverse(downPathToTargetRedList);

                    for (Coord coord: downPathToTargetRedList)
                    {
                        bufferGraphics.drawLine(coord.x, coord.y, coord.x+1, coord.y+1);
                    }
                    start.x = 0;
                }

                graphics.drawImage(buffer, 0, 0, width, height, null);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    public static void main(String[] args) {
        new PathFindTest();
    }
}
